package com.snail.clans;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.snail.commonlibrary.CallBackInterface;
import com.snail.commonlibrary.PhoneInfo;
import com.snail.commonlibrary.RequestUtil;
import com.snail.commonlibrary.SMSUtils;
import com.snail.commonlibrary.ValidationUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhaoys on 2015/9/9.
 */
public class SMSAcrivity extends Activity {

    @ViewInject(R.id.editTextPhoneNumber)
    private EditText editTextPhoneNumber;
    @ViewInject(R.id.editTextConnent)
    private EditText editTextConnent;

    @ViewInject(R.id.textviewEMI)
    private TextView textviewEMI;
    @ViewInject(R.id.textView)
    private TextView textView;

    PhoneInfo phoneInfo = null;


    private Handler handler = new Handler();

    private Runnable task = new Runnable() {
        public void run() {
            handler.postDelayed(this,5*1000);//设置延迟时间，此处是5秒
            //需要执行的代码
            requestSMSInfo();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE); // 无title
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 全屏

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 固定程序为竖屏

        setContentView(R.layout.activity_sms);
        ViewUtils.inject(this);


        phoneInfo = new PhoneInfo(this);
        textviewEMI.setText("手机唯一标识："+phoneInfo.getPhoneIMEI());
        textView.setText("手机号：" + phoneInfo.getNativePhoneNumber());

        //立即调用
        handler.post(task);
    }

    @OnClick({R.id.buttonSend})
    public void testOnClick(View view) {
        String phoneNumber = editTextPhoneNumber.getText().toString();
        String connent = editTextConnent.getText().toString();
        sendSMS(phoneNumber,connent);
    }

    /**
     * 循环发送信息到客户手机上
     */
    private void requestSMSInfo(){
        RequestUtil request = new RequestUtil("",new RequestParams(),this);
        request.url = "http://webchat.qingcailuobo.cn/webchat/rest/weixin/querySms/"+phoneInfo.getPhoneIMEI();
        request.setCallfuc(new CallBackInterface() {
            @Override
            public void success(Object temp) {
                JSONObject dataJson = null;
                try {
                    dataJson = new JSONObject(temp.toString());
                    String phoneNumber = dataJson.getString("number");
                    String connent =  dataJson.getString("msg");
                    sendSMS(phoneNumber,connent);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void failure(Object temp) {
                Toast.makeText(SMSAcrivity.this,"网络异常",Toast.LENGTH_LONG).show();
            }
        });
        request.sendGet();
    }

    /**
     * 发送短信
     * @param phoneNumber 手机号
     * @param connent  发送短信内容
     */
    private void sendSMS(String phoneNumber,String connent){
        if(phoneNumber.equals("") ||
                connent.equals("") ){
            Toast.makeText(this,"发送信息不能为空",Toast.LENGTH_LONG).show();
        }else{
            if(ValidationUtil.isMobileNO(phoneNumber)){
                SMSUtils.sendSMS(phoneNumber, connent, this);
            }else{
                Toast.makeText(this,"手机号格式不正确",Toast.LENGTH_LONG).show();
            }
        }
    }
}
